import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Vant, {Notify} from 'vant';
import 'vant/lib/index.css';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {getToken,setToken,removeToken,setShoppingToken,getShoppingToken,removeShoppingToken} from "@/utils/auth";
// 引入本地存储
import {setJsonObjectToLocal,getJsonObjectFromLocal,clearDataFromLocal} from '@/utils/localstorage'
// 引入H5头部组件
import HEADER from '@/components/home/header'
import moment from 'moment';
import Vuex from 'vuex'
import {identityIdCard} from '@/utils/validate'

Vue.use(Vuex)
Vue.use(Vant);
Vue.use(moment);
Vue.use(ElementUI,{ size: 'small', zIndex: 3000 });

// 获得用户token信息
Vue.prototype.getToken = getToken;
Vue.prototype.setToken = setToken;
Vue.prototype.removeToken = removeToken;
// 全局挂载H5标题
Vue.component('HEADER',HEADER);
/**
 * 向本地浏览器的某个对象中存储json数据
 * @param localObject 本地对象  localStorage或者sessionStorage
 * @param key
 * @param value
 */
Vue.prototype.setJsonObjectToLocal = setJsonObjectToLocal
/**
 * 根据key获取本地浏览器某个对象的存储json数据
 * @param localObject  本地对象  localStorage或者sessionStorage
 * @param key
 * @returns {DOMPoint|SVGNumber|string|SVGTransform|SVGLength|SVGPathSeg|any}
 */
Vue.prototype.getJsonObjectFromLocal = getJsonObjectFromLocal
/**
 * 清除本地key对应的数据
 * @param localObject
 * @param key
 */
Vue.prototype.clearDataFromLocal = clearDataFromLocal

Vue.config.productionTip = false

Vue.filter('dateYMDHMSFormat',function(dateStr,pattern='YYYY-MM-DD HH:mm:ss'){
  return moment(dateStr).format(pattern);
})

// 格式化时间
Vue.prototype.formatDateUtil = (dateStr,pattern)=>{
  return moment(dateStr).format(pattern);
}
// 测试环境
// Vue.prototype.webUrl = "http://172.16.10.138:8080";

// 正式环境
Vue.prototype.webUrl = "https://owaes.baiyiyl.com";

Vue.prototype.VUE_APP_BASE_API = '/prod-api'

// 通知
Vue.prototype.dialogNotifySuccess = (msg)=>{
  Notify({
    message: msg,
    duration: 5000,
    type: "success"
  });
}
Vue.prototype.dialogNotifyError = (msg)=>{
  Notify({
    message: msg,
    duration: 5000,
    type: "danger"
  });
}
Vue.prototype.dialogNotifyInfo = (msg)=>{
  Notify({
    message: msg,
    duration: 5000,
    //type: "warning",
    type: "primary"
  });
}

// 获得地址栏的 参数值
Vue.prototype.getUrlArgusString = (name)=>{
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = decodeURI(window.location.search).substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}
// 计算年龄
Vue.prototype.calcSexAndAge = function(cardNum,form){
  if(identityIdCard(cardNum)){
    /*// 计算性别和年龄
    //获取出生日期
    cardNum.substring(6, 10) + "-" + cardNum.substring(10, 12) + "-" + cardNum.substring(12, 14);*/
    // 获取性别
    if (parseInt(cardNum.substr(16, 1)) % 2 == 1) {
      form.sex = '男';
    } else {
      form.sex = '女';
    }
    //获取年龄
    let myDate = new Date();
    let month = myDate.getMonth() + 1;
    let day = myDate.getDate();
    let age = myDate.getFullYear() - cardNum.substring(6, 10) - 1;
    if (
        cardNum.substring(10, 12) < month ||
        (cardNum.substring(10, 12) == month && cardNum.substring(12, 14) <= day)
    ) {
      age++;
    }
    form.age = age;
  }else{
    console.log("身份证验证失败","idCard");
  }
}
// 路由拦截
router.beforeEach((to, from, next) => {
  let adminToken = getToken();
  console.log('全局拦截中得loginToken',adminToken);

  if (to.meta.login) {
    if (!adminToken) {
      return next({
        path: '/login'
      })
    }
  }

  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})
// 全局共享数据
const store = new Vuex.Store({
  state: {
    menuIsShow: true,
    titleName: '学历报名'
  },
  mutations: {
    hiddenMenu (state) {// 隐藏菜单
      state.menuIsShow = false
    },
    showMenu (state) {// 显示菜单
      state.menuIsShow = true
    },
    setTitleName(state,titleName){
      state.titleName = titleName
    },
  },
  getters: {
    getTitleName: state => {
      return state.titleName;
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
